package com.rky.spring.cloud.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 0x_Jss
 * @date 2022年02月19日 18:30
 * @description
 */
@Component
public class MyZuulFilter extends ZuulFilter {

    Logger logger = LoggerFactory.getLogger(MyZuulFilter.class);

    @Override
    public String filterType() {
        String filterType = "pre";
        return filterType;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 判断当前请求是否要进行过滤
     * 过滤：返回：true 继续执行run()方法
     * 不过滤：返回：false 直接放行
     * @return
     */
    @Override
    public boolean shouldFilter() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String parameter = request.getParameter("signal");
        return "hello".equals(parameter);
    }

    @Override
    public Object run() throws ZuulException {
        logger.info("当前请求要进行过滤，run()方法执行了");
        return null;
    }
}